package mensaplan;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import de.app.badresden.R;

import android.content.Context;

import utils.ParserUtil;

public class MensaMahlzeitenImport {

	public ArrayList<MensaMahlzeit> ImportMensa(Context context) {

		ArrayList<MensaMahlzeit> essen = new ArrayList<MensaMahlzeit>();

		essen.addAll(ImportMensaSite(context.getString(R.string.url_mensa1)));
		essen.addAll(ImportMensaSite(context.getString(R.string.url_mensa2)));
		//essen.addAll(ImportMensaSite(context.getString(R.string.url_mensa3)));

		return essen;
	}

	private List<MensaMahlzeit> ImportMensaSite(String Url) {

		ArrayList<MensaMahlzeit> essen = new ArrayList<MensaMahlzeit>();

		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(Url);
			HttpResponse response = httpClient.execute(httpget);
			HttpEntity entity = response.getEntity();

			String regex = "<thead><tr><th class=\"text\">(.*?)</th>.*?</thead><tbody><tr.*?>(.*?)</tr></tbody>";

			for (Matcher m = Pattern.compile(regex)
					.matcher(
							ParserUtil.htmlEncoding(ParserUtil.cleanHtml(EntityUtils
									.toString(entity)))); m.find();)
				essen.addAll(getMeal(ParserUtil.getDate(m.toMatchResult().group(1)), m
						.toMatchResult().group(2)));

		}
		catch (ClientProtocolException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}

		return essen;
	}

	private ArrayList<MensaMahlzeit> getMeal(Date date, String meal) {

		ArrayList<MensaMahlzeit> essen = new ArrayList<MensaMahlzeit>();
		Calendar currentCalendar = Calendar.getInstance();
		currentCalendar.setTime(date);

		Matcher m = Pattern.compile("<td.*?class=\"keinangebot\">(.*?)</td>").matcher(meal);

		if (m.find()) {
			MensaMahlzeit currentMeal = new MensaMahlzeit();
			currentMeal.setDatum(currentCalendar);
			currentMeal.setBeschreibung(m.toMatchResult().group(1));
			currentMeal.setAusverkauft(true);
			essen.add(currentMeal);

			return essen;
		}

		String regex = "<td class=\"text\"><a.*?>(.*?)</a>.*?<td class=\"preise\"><a href=\"details-(.*?).html.pni=.*?\">(.*?)<.*?>";

		for (m = Pattern.compile(regex).matcher(meal); m.find();) {
			MensaMahlzeit currentMeal = new MensaMahlzeit();
			String currentPreis;

			currentMeal.setDatum(currentCalendar);
			currentMeal.setBeschreibung(m.toMatchResult().group(1));
			currentMeal.setUrlPicture(getPicureLink(m.toMatchResult().group(2),
					currentCalendar));
			currentPreis = m.toMatchResult().group(3);
			currentPreis = currentPreis.replaceAll(" &euro; .*? &euro;", "");

			if (currentPreis.contains("ausverkauft")) currentMeal.setAusverkauft(true);
			else
				currentMeal.setPreis(Double.valueOf(currentPreis.replace(",", "."))
						.doubleValue());

			essen.add(currentMeal);
		}

		return essen;
	}

	private String getPicureLink(String Number, Calendar date) {

		String currentValue = String.format(
				"http://bilderspeiseplan.studentenwerk-dresden.de/m32/%d%d/thumbs/%s.jpg",
				date.get(Calendar.YEAR), (date.get(Calendar.MONTH) + 1), Number);
		return currentValue;
	}

}
